<template>
  <div class="app-container">
    <el-button
      :loading="downloadLoading"
      style="margin-bottom: 20px;"
      type="primary"
      :icon="Document"
      @click="handleDownload"
    >
      Export
    </el-button>
    <el-table
      ref="multipleTable"
      v-loading="listLoading"
      :data="list"
      element-loading-text="loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="Id" width="95">
        <template #default="scope">
          {{ scope.$index }}
        </template>
      </el-table-column>
      <el-table-column label="Main InforMation" align="center">
        <el-table-column label="Title">
          <template #default="scope">
            {{ scope.row.title }}
          </template>
        </el-table-column>
        <el-table-column label="Author" width="110" align="center">
          <template #default="scope">
            <el-tag>{{ scope.row.author }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="Reading" width="115" align="center">
          <template #default="scope">
            {{ scope.row.pageviews }}
          </template>
        </el-table-column>
      </el-table-column>
      <el-table-column align="center" label="Date" width="220">
        <template #default="scope">
          <el-icon style="vertical-align: middle;">
            <Timer />

          </el-icon>
          <span>{{ parseTime(scope.row.timestamp, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { parseTime } from '@/utils'
import { fetchList } from '@/api/article'
import { Document, Timer } from '@element-plus/icons-vue'

const list = ref(null)
const listLoading = ref(true)
const downloadLoading = ref(false)
const listQuery = reactive({
  page: 1,
  limit: 20
})

const fetchData = () => {
  listLoading.value = true
  fetchList(listQuery).then(response => {
    list.value = response.data.items
    listLoading.value = false
  })
}

const handleDownload = () => {
  downloadLoading.value = true
  import('@/vendor/Export2Excel').then(excel => {
    const multiHeader = [['Id', 'Main Information', '', '', 'Date']]
    const header = ['', 'Title', 'Author', 'Readings', '']
    const filterVal = ['id', 'title', 'author', 'pageviews', 'display_time']
    const data = formatJson(filterVal, list.value)
    const merges = ['A1:A2', 'B1:D1', 'E1:E2']
    excel.export_json_to_excel({
      multiHeader,
      header,
      merges,
      data
    })
    downloadLoading.value = false
  })
}

const formatJson = (filterVal, jsonData) => {
  return jsonData.map(v => filterVal.map(j => {
    if (j === 'timestamp') {
      return parseTime(v[j])
    } else {
      return v[j]
    }
  }))
}

fetchData()
</script>

<script>
export default {
  name: 'MergeHeader'
}
</script>

<style scoped>

</style>
